{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "cb152da0-3266-458e-812b-e76fbdc4eec7",
   "metadata": {},
   "source": [
    "# Onboarding"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b5fe55c3-63c8-4679-88ff-986ed532dfc5",
   "metadata": {},
   "source": [
    "## [MarshData](https://github.com/suarez96/AP-bIO/blob/main/Data.py) object"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b9742301-5112-4547-ae79-19a74772bea6",
   "metadata": {},
   "outputs": [],
   "source": [
    "from Data import MarshData, FantasiaData\n",
    "from Transforms import WPC\n",
    "import Transforms\n",
    "from Signal import Signal\n",
    "import os\n",
    "import numpy as np\n",
    "\n",
    "marsh_dataset = [\n",
    "    MarshData(f\"../MARSH/{i}/\") for i in os.listdir(\"../MARSH/\") if len(i) == 4\n",
    "]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cdaf22ce-775c-42b8-aab0-ee45cbe4572c",
   "metadata": {},
   "source": [
    "## Working with the [Signal](https://github.com/suarez96/AP-bIO/blob/main/Signal.py#L10) object"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a80ae162-af1b-4a17-8544-e5b19d5c9fea",
   "metadata": {},
   "source": [
    "### Plot the entire ECG for the first sample\n",
    "Might take some time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "63ed1ac5-51fb-485c-bbf9-b60fcb30cd71",
   "metadata": {},
   "outputs": [],
   "source": [
    "marsh_dataset[0].ECG().plot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eb460df6-90a5-46d3-9e83-6c2ab67e753f",
   "metadata": {},
   "outputs": [],
   "source": [
    "marsh_dataset[0].ECG().fft(top_freq=125)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6af9cea7-6253-4721-b235-16823c639eae",
   "metadata": {},
   "source": [
    "### Access the raw ECG data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "18db00df-a5da-4a04-bcc4-c9eba202a613",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "marsh_dataset[0].ECG().data.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7e942e2a-ea48-496b-b1cb-58ae2fe135bf",
   "metadata": {},
   "source": [
    "## Working with [Transforms](https://github.com/suarez96/AP-bIO/blob/main/Transforms.py#L54)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ebe247d1-bd11-464c-8aa8-4ca1576b484b",
   "metadata": {},
   "source": [
    "### Initialize the transform in question"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dc53eede-538f-4b44-8dbb-3f68bb1b7391",
   "metadata": {},
   "outputs": [],
   "source": [
    "crop = Transforms.Crop(start=60, end=69)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e14b2249-db9c-4296-bafe-b9c457da01d2",
   "metadata": {},
   "source": [
    "### Apply the transform"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9090090d-2e48-4153-8e5f-1c8ff9051775",
   "metadata": {},
   "outputs": [],
   "source": [
    "marsh_dataset[0].ECG().transform(\n",
    "    transforms = [crop]\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d1fe554c-c20e-4d9f-bb5a-06edc800372d",
   "metadata": {},
   "source": [
    "### Access the transformed data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cf0eb449-a151-42d9-8b9e-a2a588227277",
   "metadata": {},
   "outputs": [],
   "source": [
    "marsh_dataset[0].ECG().transformed_data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ba3d2f0f-0817-4db0-9987-548a2c60c451",
   "metadata": {},
   "source": [
    "### Plot the transformed data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "61da24c1-be1c-43f2-912f-23c5923db1db",
   "metadata": {},
   "outputs": [],
   "source": [
    "marsh_dataset[0].ECG().plot(transformed=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4d325fa6",
   "metadata": {},
   "outputs": [],
   "source": [
    "### Now Plotting IP to for the same crop\n",
    "marsh_dataset[0].IP().transform(\n",
    "    transforms = [crop]\n",
    ")\n",
    "marsh_dataset[0].IP().transformed_data\n",
    "marsh_dataset[0].IP().plot(transformed=True)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "837cdee7",
   "metadata": {},
   "outputs": [],
   "source": [
    "marsh_dataset[0].ECG().plot(transformed=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "299dab3e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "\n",
    "fig, ax1 = plt.subplots()\n",
    "\n",
    "# axis for ECG\n",
    "ax1.plot(marsh_dataset[0].ECG().transformed_data, color='blue')\n",
    "ax1.set_ylabel('ECG', color='blue')\n",
    "\n",
    "# axis for IP\n",
    "ax2 = ax1.twinx()\n",
    "\n",
    "ax2.plot(marsh_dataset[0].IP().transformed_data, color='red')\n",
    "ax2.set_ylabel('IP', color='red')\n",
    "\n",
    "ax1.set_xlabel('Samples')\n",
    "plt.title('ECG and IP')\n",
    "fig.set_size_inches(10, 8)\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a5d77307",
   "metadata": {},
   "outputs": [],
   "source": [
    "from Signal import Signal\n",
    "from Transforms import MeanSubtraction, Detrend\n",
    "import Models\n",
    "import numpy\n",
    "\n",
    "# FFT\n",
    "def apply_fft(signal):\n",
    "    data = signal.data\n",
    "    data -= np.mean(data)\n",
    "    fft_data = np.fft.fft(data)\n",
    "    freq = np.fft.fftfreq(data.shape[0], d=1/250)\n",
    "    return fft_data, freq\n",
    "\n",
    "#crop out unwanted frequencies\n",
    "def crop_frequencies(fft_data, freq, low_cutoff=None, high_cutoff=None):\n",
    "    cropped_fft = fft_data.copy()\n",
    "    if low_cutoff is not None:\n",
    "        cropped_fft[(freq < low_cutoff)] = 0\n",
    "    if high_cutoff is not None:\n",
    "        cropped_fft[(freq > high_cutoff)] = 0\n",
    "    return cropped_fft\n",
    "\n",
    "# Plotting\n",
    "def plot_signal(signal, title='Signal', xlabel='Samples', ylabel='Amplitude', xlim=None):\n",
    "    plt.figure(figsize=(12, 6))\n",
    "    plt.plot(signal)\n",
    "    plt.title(title)\n",
    "    plt.xlabel(xlabel)\n",
    "    plt.ylabel(ylabel)\n",
    "    plt.xlim(xlim)\n",
    "    plt.grid(True)\n",
    "    plt.show()\n",
    "\n",
    "# Main code\n",
    "ecg = marsh_dataset[0].ECG().transformed_data\n",
    "plot_signal(ecg, title='Original ECG Signal',xlim=(0, 400))\n",
    "fft_data, freq = apply_fft(ecg)\n",
    "plot_signal(signal=abs(fft_data), title='ECG Signal', xlabel='Frequencies')\n",
    "\n",
    "# cut-off frequencies\n",
    "low_cutoff = 0.5\n",
    "high_cutoff = 50.0\n",
    "\n",
    "# crop\n",
    "cropped_fft = crop_frequencies(fft_data, freq, low_cutoff=low_cutoff, high_cutoff=high_cutoff)\n",
    "plot_signal(signal=fft_data, title='ECG Signal', xlabel='Frequencies',xlim=(0, 500))\n",
    "\n",
    "# inverse FFT\n",
    "cropped_signal = np.fft.ifft(cropped_fft).real\n",
    "\n",
    "\n",
    "# original and cropped signals\n",
    "plot_signal(cropped_signal, title='cropped ECG Signal',xlim=(0, 400))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ee49590b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
